﻿Imports DevExpress.XtraEditors
Imports System.Net.Sockets
Imports System.IO
Imports System.Net

Public Class frmDBOption
    Private si As Integer
    Sub view_database(ByVal host As String, ByVal port As Integer, ByVal username As String, ByVal password As String)
        Try
            Dim data As DataTable = mysql_show_databases(False, host, port, username, password)
            ListBoxControlDatabase.DataSource = data
            ListBoxControlDatabase.DisplayMember = "Database"
            ListBoxControlDatabase.ValueMember = "Database"
        Catch ex As Exception

        End Try
    End Sub


    Private Sub frmDBOption_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If File.Exists(My.Application.Info.DirectoryPath.ToString & "\client.xml") = False Then client_write_configuration()
        client_read_configuration()
        If mysql_isconnected() Then
            view_database(dbHost, dbPort, dbUser, dbPass)
            txtHost.Text = dbHost
            txtPort.Text = dbPort
            txtUsername.Text = dbUser
            txtPassword.Text = dbPass
            txtServerHost.Text = ip
            txtServerPort.Text = port
            ListBoxControlDatabase.SelectedValue = dbName
            Dim pt As New Point(0, 100)
            ListBoxControlDatabase.AutoScrollOffset = pt
            si = ListBoxControlDatabase.SelectedIndex
            SimpleButtonConnect.Text = "Change"

            txtHost.Enabled = False
            txtPort.Enabled = False
            txtUsername.Enabled = False
            txtPassword.Enabled = False
            SimpleButtonSave.Enabled = False

            txtServerHost.Enabled = False
            txtServerPort.Enabled = False
        Else
            txtHost.Text = dbHost
            txtPort.Text = dbPort
            txtUsername.Text = dbUser
            txtPassword.Text = dbPass
            txtServerHost.Text = ip
            txtServerPort.Text = port

            txtHost.Enabled = True
            txtPort.Enabled = True
            txtUsername.Enabled = True
            txtPassword.Enabled = True
            SimpleButtonSave.Enabled = False

            txtServerHost.Enabled = True
            txtServerPort.Enabled = True
        End If
    End Sub

    Private Sub SimpleButtonConnect_Click(sender As Object, e As EventArgs) Handles SimpleButtonConnect.Click
        If SimpleButtonConnect.Text = "Change" Then

            SimpleButtonConnect.Text = "Connect"
            ListBoxControlDatabase.SelectedIndex = si
            SimpleButtonSave.Enabled = True
            txtHost.Enabled = True
            txtPort.Enabled = True
            txtUsername.Enabled = True
            txtPassword.Enabled = True

            txtServerHost.Enabled = True
            txtServerPort.Enabled = True
        Else
            If mysql_isconnected(False, txtHost.Text, Convert.ToInt32(txtPort.Text), txtUsername.Text, txtPassword.Text) Then
                view_database(txtHost.Text, Convert.ToInt32(txtPort.Text), txtUsername.Text, txtPassword.Text)
                SimpleButtonSave.Enabled = True
            End If
        End If
    End Sub

    Private Sub SimpleButtonSave_Click(sender As Object, e As EventArgs) Handles SimpleButtonSave.Click
        If mysql_isconnected(False, txtHost.Text, Convert.ToInt32(txtPort.Text), txtUsername.Text, txtPassword.Text) And ListBoxControlDatabase.SelectedIndex > -1 And ListBoxControlDatabase.ItemCount > 0 Then
            If File.Exists(My.Application.Info.DirectoryPath.ToString & "\client.xml") = False Then client_write_configuration()
            client_update_configuration(New Object(0) {}, reconfig_server(txtServerHost.Text, txtServerPort.Text, username, password, auto_login), reconfig_database(txtHost.Text, txtPort.Text, txtUsername.Text, txtPassword.Text, ListBoxControlDatabase.SelectedValue.ToString))
            client_read_configuration()
            txtHost.Enabled = False
            txtPort.Enabled = False
            txtUsername.Enabled = False
            txtPassword.Enabled = False

            txtServerHost.Enabled = False
            txtServerPort.Enabled = False

            SimpleButtonSave.Enabled = False
            SimpleButtonConnect.Text = "Change"
            si = ListBoxControlDatabase.SelectedIndex
        Else
            MsgBox("Cannot Save Configuration" & vbCrLf & "Please Check if Database is still connected and you have selected one database from Database List", MsgBoxStyle.Exclamation, "Warning")
        End If
    End Sub

    Private Sub ListBoxControlDatabase_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBoxControlDatabase.SelectedIndexChanged
        If SimpleButtonConnect.Text = "Change" Then
            ListBoxControlDatabase.SelectedIndex = si
        End If
    End Sub



    Private Sub SimpleButtonCheckServer_Click(sender As Object, e As EventArgs) Handles SimpleButtonCheckServer.Click
        Dim TestSocket As Connections
        Dim state As Boolean = True
        Cursor = Cursors.WaitCursor
        If txtServerHost.Text.Equals("") = False And txtServerPort.Text.Equals("") = False Then
            Try
                'Dim HostEntry As IPHostEntry = Dns.GetHostEntry(txtServerHost.Text)
                Dim ipAddr As IPAddress() = Dns.GetHostAddresses(txtServerHost.Text) 'HostEntry.AddressList
                Dim i As Integer = 0
                Dim indexTmp As String = -1

                For i = 0 To ipAddr.Count - 1
                    Select Case ipAddr(i).AddressFamily
                        Case System.Net.Sockets.AddressFamily.InterNetwork
                            If indexTmp = -1 Then
                                indexTmp = i
                            End If
                            If ipAddr(i).ToString.Equals(txtServerHost.Text) Then
                                Exit For
                            End If
                    End Select
                    If indexTmp > -1 And i >= ipAddr.Count - 1 Then
                        i = indexTmp
                        Exit For
                    End If
                Next

                TestSocket = New Connections(ipAddr(i), CType(txtServerPort.Text, Integer))
            Catch ex As Exception
                MsgBox("Can't Connect To Defined Host", MsgBoxStyle.Critical, "Error")
                state = False
            End Try

            If state Then
                MsgBox("You Can Try to Using This Host", MsgBoxStyle.Information, "Connection OK")
            End If
        Else
            MsgBox("Please fill server information correctly", MsgBoxStyle.Exclamation, "Warning")
        End If
        Cursor = Cursors.Default
    End Sub
End Class